package Demo04.Demo03.Demo02;
 
import java.util.*;
 
public class Demo03_Set {
 
public static void main(String[] args) {
 
    TreeSet<Student> ts=new TreeSet<Student>(new Com());
    //添加元素进去
    for(int i=20070301,j=1;i<=20070330;i++,j++)
    {
        ts.add(new Student(i,"同学"+j,(int) (40*Math.random()+60)));
    }
 
    //迭代循环取出
    Iterator<Student> it=ts.iterator();
    while(it.hasNext())
    {
        Student o1=it.next();
        System.out.println("学号："+o1.num+"  "+"姓名："+o1.name+"  "+"  "+"成绩："+o1.grade);
 
    }
 
}
}
//学生类
class Student 
{
int num;
int grade;
String name;
 
 
public Student(int num, String name,int grade)
{
    this.num=num;
    this.name=name;
    this.grade=grade;
}
}
class Com implements Comparator
{
 
@Override
public int compare(Object o1, Object o2) {
 
    Student s1=(Student) o1;
    Student s2=(Student) o2;
    if(s1.grade>s2.grade)
        return 1;
    if(s1.grade<s2.grade)
        return -1;
    if(s1.grade==s2.grade)
    {
        return new Integer(s1.num).compareTo(new Integer(s2.num));
    }
    return 0;
}
}